﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aribaccio
{
    /// <summary>
    /// 应用程序路径辅助类
    /// </summary>
    public class PathHelper
    {
        /// <summary>
        /// 应用程序根目录
        /// </summary>
        protected static string AppPath { get; set; }

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static PathHelper()
        {
            AppPath = GetAppStartupDirectory();
        }

        /// <summary>
        /// 获取应用程序根目录
        /// </summary>
        /// <returns></returns>
        public static string GetAppDir()
        {
            if (string.IsNullOrEmpty(AppPath))
            {
                AppPath = GetAppStartupDirectory();
            }
            return AppPath;
        }

        /// <summary>
        /// 获取应用程序根目录下指定目录的全路径
        /// </summary>
        /// <returns></returns>
        public static string GetPathFullDirectory(string directoryPathName)
        {
            string strResult = Path.Combine(GetAppDir(), directoryPathName);
            //检测末尾特殊字符
            if (!strResult.EndsWith(Path.DirectorySeparatorChar.ToString()))
                strResult = strResult + Path.DirectorySeparatorChar;
            //检测文件夹是否存在，并创建
            if (!Directory.Exists(strResult))
                Directory.CreateDirectory(strResult);
            return strResult;
        }

        /// <summary>
        /// 获取应用程序根目录下指定文件的全路径
        /// </summary>
        /// <param name="filePathName"></param>
        /// <returns></returns>
        public static string GetPathFullFileName(string filePathName)
        {
            return Path.Combine(GetAppDir(), filePathName);
        }

        /// <summary>
        /// 应用程序启动根目录
        /// </summary>
        /// <returns></returns>
        protected static string GetAppStartupDirectory()
        {
            var startupPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string dir = Path.GetDirectoryName(startupPath);
            return dir;
        }
    }
}
